package util.system;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;



public class ExportToExcel {
	static HSSFWorkbook wb;
	static HSSFRow row;
	static HSSFSheet sheet;
	private String sheetName;
	private String[] columnHeader;
	private int startRowData;
	private int startRowColumnHeader;
	private String xlsFileName;
	private List<?> gridModel;
	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public String[] getColumnHeader() {
		return columnHeader;
	}

	public void setColumnHeader(String[] columnHeader) {
		this.columnHeader = columnHeader;
	}

	public ExportToExcel() {
		wb = new HSSFWorkbook();

	}

	
	
	public ByteArrayInputStream generateStandardExcel() {
		sheet = wb.createSheet(getSheetName());
		row = sheet.createRow(getStartRowColumnHeader());
		int colindex = 0;

		CellStyle tCellStyle = wb.createCellStyle();

		Font tFont = wb.createFont();
		tFont.setFontName("Tahoma");
		tFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		tCellStyle.setFont(tFont);

		for (String ch : getColumnHeader()) {

			Cell tCell = row.createCell(colindex);
			tCell.setCellValue(ch);
			tCell.setCellStyle(tCellStyle);
			colindex++;
			sheet.autoSizeColumn(colindex); // adjust width of the first column

		}

		try {
			ByteArrayOutputStream xlsout = new ByteArrayOutputStream();
			wb.write(xlsout);
			return new ByteArrayInputStream(xlsout.toByteArray());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public int getStartRowData() {
		return startRowData;
	}

	public void setStartRowData(int startRowData) {
		this.startRowData = startRowData;
	}

	public int getStartRowColumnHeader() {
		return startRowColumnHeader;
	}

	public void setStartRowColumnHeader(int startRowColumnHeader) {
		this.startRowColumnHeader = startRowColumnHeader;
	}

	public String getXlsFileName() {
		return xlsFileName;
	}

	public void setXlsFileName(String xlsFileName) {
		this.xlsFileName = xlsFileName;
	}

	public List<?> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<?> gridModel) {
		this.gridModel = gridModel;
	}

}
